package com.gzhryc.log.dao;

import java.sql.SQLException;
import java.util.List;

import com.gzhryc.jdbc.BaseDao;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Query;
import com.gzhryc.log.dao.db.HttpVisitRecord;
import com.gzhryc.log.dao.models.HttpVisitCountDay;

public class HttpVisitRecordDao extends BaseDao<HttpVisitRecord>{

	public HttpVisitRecordDao(String jdbcKey) {
		super(jdbcKey);
	}


	public Long countDistinctIpByCondition(Conditions condition) throws SQLException{
		String sql = "select count(distinct ip) from sys_http_link_record";
		Query query = new Query(sql);
		query.appendSql(condition.toSql(query.getParams(), true));
		return this.jdbcHelper.getFirstField(query, Long.class);
	}
	
	/**
	 * 统计HTTP每天的访问次数
	 * @param condition
	 * @return
	 * @throws SQLException
	 */
	public List<HttpVisitCountDay> countGroupDay(Conditions condition) throws SQLException{
		String sql = "select day,count(1) as total from sys_http_link_record";
		Query query = new Query(sql);
		query.appendSql(condition.toSql(query.getParams(), true));
		query.appendSql(" group by day order by day asc");
		return this.jdbcHelper.find(query, HttpVisitCountDay.class);
	}
	
	/**
	 * 统计HTTP每天的IP访问次数
	 * @param condition
	 * @return
	 * @throws SQLException
	 */
	public List<HttpVisitCountDay> countDistinctIpGroupDay(Conditions condition) throws SQLException{
		String sql = "select day,count(distinct ip) as total from sys_http_link_record";
		Query query = new Query(sql);
		query.appendSql(condition.toSql(query.getParams(), true));
		query.appendSql(" group by day order by day asc");
		return this.jdbcHelper.find(query, HttpVisitCountDay.class);
	}
}
